import { GAlertWin } from "../../../../../com/mui/controls/GAlertWin";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { BaseWindow } from "../../../../component/window/BaseWindow";
import { Vector } from "../../../../../__AS3__/vec/Vector";
type int = number;
//class TimerAlertWin
    
    export  class TimerAlertWin extends GAlertWin
    {
        constructor(arg1: number, arg2: number)
        {
            super(arg1, arg2);
            this._timeOutHandler = Alert.timerOutHandler;
            this._needCallCloseHandler = Alert.needCallCloseHandler;
            addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemovedFromStageHandler);
            return;
        }

        /* internal  */createTimer(): void
        {
            if (Alert.timerOut <= 0) 
            {
                return;
            }
            if (!this._secTimer) 
            {
                this._secTimer = new SecTimer(1, Alert.timerOut);
                this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            }
            this._secTimer.repeatCount = Alert.timerOut;
            if (!this._secTimer.running) 
            {
                this._secTimer.start();
            }
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            if (this.parent) 
            {
                this.parent.removeChild(this);
            }
            if (this._timeOutHandler != null) 
            {
                this._timeOutHandler(extendObj);
            }
            if (!(closeHandler == null) && this._needCallCloseHandler) 
            {
                if (this._timeNO) 
                {
                    if (extendObj == null) 
                    {
                        closeHandler(Alert.CANCEL);
                    }
                    else 
                    {
                        closeHandler(Alert.CANCEL, extendObj);
                    }
                }
                else if (extendObj == null) 
                {
                    closeHandler(Alert.OK);
                }
                else 
                {
                    closeHandler(Alert.OK, extendObj);
                }
            }
            return;
        }

        /* internal  */onEnterFrameHandler(arg1: SecTimer): void
        {
            var loc1=null;
            if (this._timeNO) 
            {
                loc1 = this.getBtnByName(Alert.CANCEL);
            }
            else 
            {
                loc1 = this.getBtnByName(Alert.OK);
            }
            if (loc1 != null) 
            {
                loc1.label = this._btnLabel + "(" + arg1.repeatCount + ")";
            }
            return;
        }

        /* internal  */onRemovedFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._secTimer && this._secTimer.running) 
            {
                this._secTimer.stop();
                this._secTimer.isDelete = false;
            }
            return;
        }

        /* internal  */getBtnByName(arg1: int): GButton
        {
            var loc1=null;
            var loc2=0;
            var loc3=allButtons;
            for(loc1 of loc3) 
            {
                if (loc1.name != "btn_" + arg1) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        protected /* override */ createChildren(): void
        {
            if (Alert.title == null) 
            {
                title = GlobalClass.getBitmap(ImagesConst.Title_Tips);
            }
            this._timeNO = Alert.timerNO;
            if (this._timeNO) 
            {
                this._btnLabel = Alert.calcelLabel;
                Alert.calcelLabel = Alert.calcelLabel + ("(" + Alert.timerOut + ")");
            }
            else 
            {
                this._btnLabel = Alert.okLabel;
                Alert.okLabel = Alert.okLabel + ("(" + Alert.timerOut + ")");
            }
            super.createChildren();
            return;
        }

        protected /* override */ childrenCreated(): void
        {
            var loc1=null;
            var loc10=null;
            if (typeof !(Alert.title === "string")) 
            {
                if (Alert.title) 
                {
                    title = Alert.title;
                }
                else 
                {
                    title = GlobalClass.getBitmap("Title_Tips");
                }
            }
            message.multiline = true;
            message.htmlText = Alert.text;
            loc1 = new Object /* flash.display.Sprite */();
            var loc2=allButtons.length;
            var loc3=0;
            while (loc3 < loc2) 
            {
                (loc10 = allButtons[loc3]).addEventListener(flash.events.MouseEvent.CLICK, buttonClickHandler);
                loc1.addChild(loc10);
                loc10.x = loc3 * (Alert.buttonWidth + 25);
                ++loc3;
            }
            var loc4;
            (loc4 = new Object /* flash.display.Sprite */()).addChild(message);
            if (icon) 
            {
                loc4.addChild(icon);
                message.x = icon.width + 15;
            }
            var loc5=boarderWidth * 2 + 10;
            var loc6;
            if ((loc6 = Math.max(title.width + loc5, message.width + loc5, loc1.width + loc5, minWidth)) < 260) 
            {
                loc6 = 260;
            }
            var loc7=60 + message.height + Alert.buttonHeight + 45;
            var loc8;
            (loc8 = ResouceConst.getScaleBitmap("WindowCenterB")).width = loc6 - 34;
            loc8.height = loc7 - 52;
            loc8.x = 17;
            loc8.y = 41;
            message.x = loc8.x + loc8.width / 2 - message.width / 2;
            message.y = 55;
            loc1.x = (loc6 - (Alert.buttonWidth + 25) * allButtons.length + 25) / 2;
            loc1.y = 60 + message.height + 15;
            boarder.width = loc6;
            boarder.height = 150;
            _promptBackground = new BaseWindow();
            (_promptBackground as BaseWindow).titleHeight = 30;
            (_promptBackground as BaseWindow).closeBtn.visible = false;
            var loc9;
            if ((loc9 = getButtonFlagsList())[3] != 1) 
            {
            };
            prompt = new Object /* flash.display.Sprite */();
            prompt.addChild(_promptBackground);
            title.y = (30 - title.height) / 2;
            title.x = (loc6 - title.width) / 2;
            (_promptBackground as BaseWindow).setSize(loc6, loc7);
            if (typeof Alert.title === "string") 
            {
                (_promptBackground as BaseWindow).title = Alert.title;
            }
            addChild(background);
            prompt.addChild(title);
            prompt.addChild(loc8);
            prompt.addChild(message);
            if (icon) 
            {
                addChild(icon);
            }
            prompt.addChild(loc1);
            addChild(prompt);
            prompt.x = (stageWidth - prompt.width) / 2;
            prompt.y = (stageHeight - prompt.height) / 2;
            this.createTimer();
            return;
        }

        protected /* override */ createButtons(): Vector.<GButton>
        {
            var loc1=new Vector.<GButton>();
            var loc2=getButtonFlagsList();
            if (loc2[0] == 1) 
            {
                loc1.push(this.CreateButton(Alert.yesLabel, Alert.YES));
            }
            if (loc2[1] == 1) 
            {
                loc1.push(this.CreateButton(Alert.noLabel, Alert.NO));
            }
            if (loc2[2] == 1) 
            {
                loc1.push(this.CreateButton(Alert.okLabel, Alert.OK));
            }
            if (loc2[3] == 1) 
            {
                loc1.push(this.CreateButton(Alert.calcelLabel, Alert.CANCEL));
            }
            return loc1;
        }

        /* internal  */CreateButton(arg1: string, arg2: int): GButton
        {
            var loc1=new GButton();
            loc1.label = arg1;
            loc1.width = Alert.buttonWidth;
            loc1.height = Alert.buttonHeight;
            loc1.name = "btn_" + arg2;
            Alert.buttonStyleName = "Button";
            loc1.styleName = Alert.buttonStyleName;
            return loc1;
        }

        /* internal */ /* var */_secTimer: SecTimer;

        /* internal */ /* var */_btnLabel: string;

        /* internal */ /* var */_timeNO: Boolean;

        /* internal */ /* var */_timeOutHandler: Function;

        /* internal */ /* var */_needCallCloseHandler: Boolean;
    }
